require(tidyverse)

rm(list = ls())
gc()

################################################################################
##
## Date:    2024-12-20
## Author:  james.h.bisbee@vanderbilt.edu
## Purpose: This script generates Figure 3.
## Inputs:  ./data/anonymized_fig3.RData
##            - Gallup daily data merged with county-level information (PROPRIETARY)
## Outputs: ./figures/fig3.pdf
##
################################################################################

# Compute details
print(paste0('Compute environment from ',Sys.Date(),' run by Bisbee'))
if(Sys.info()['sysname'] == 'Windows') {
  ram_size = system("wmic MemoryChip get Capacity", intern = TRUE)[-1]
  model_name = system("wmic cpu get name", intern = TRUE)[2] # nocov
  vendor_id = system("wmic cpu get manufacturer", intern = TRUE)[2] # nocov
  
  print(list(ram = stringr::str_squish(ram_size)[1],
             vendor_id = stringr::str_squish(vendor_id),
             model_name = stringr::str_squish(model_name),
             no_of_cores = parallel::detectCores()))
} else if(Sys.info()['sysname'] == 'Linuxs') {
  splitted <- strsplit(system("ps -C rsession -o %cpu,%mem,pid,cmd", intern = TRUE), " ")
  df <- do.call(rbind, lapply(splitted[-1], 
                              function(x) data.frame(
                                cpu = as.numeric(x[2]),
                                mem = as.numeric(x[4]),
                                pid = as.numeric(x[5]),
                                cmd = paste(x[-c(1:5)], collapse = " "))))
  df
} else {
  cat("If not on Linux or Windows, you'll have to figure out your own solution to seeing the compute environment.")
}

sessionInfo()

load('./data/anonymized_fig3.RData')

pdf('./figures/fig3.pdf',width = 8,height = 6)
toplot %>%
  ggplot(aes(x = INT_DATE,y = ECON,color = factor(PARTY))) + 
  geom_point(alpha = .1) + 
  theme_bw() +
  geom_vline(xintercept = as.Date(c('2009-01-21','2017-01-21')),linetype = 'dashed') +  
  scale_color_manual(values = c('darkblue','darkred')) +
  scale_y_continuous(breaks = 1:4,labels = c('Poor','Only fair','Good','Excellent'),limits = c(1,4)) + 
  scale_size_continuous(range = c(.1,4),breaks = c(100,250,500)) +
  labs(x = 'Interview Date',
       title = 'Daily average views by party ID',
       subtitle = 'Economic conditions in the country',
       color = 'Party',
       size = '# Responding',
       y = 'How would you rate economic conditions in this country today?') + 
  theme(legend.position = 'bottom') + 
  guides(colour = guide_legend(override.aes = list(alpha = 1,size = 2)))
dev.off()

# EOF